/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.portmgr.core.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hxg
 */
public enum DaoSource {

    DeviceModelDAO(DeviceModelDAO.class) {
        @Override
        public boolean isActive() {
            return true;
        }
    }, DeviceDAO(DeviceDAO.class) {
        @Override
        public boolean isActive() {
            return true;
        }
    }, IntfDAO(IntfDAO.class) {
        @Override
        public boolean isActive() {
            return true;
        }
    }, LineDAO(LineDAO.class) {
        @Override
        public boolean isActive() {
            return true;
        }
    }, LineProfileDAO(LineProfileDAO.class) {
        @Override
        public boolean isActive() {
            return true;
        }
    };

    private DaoSource(Class<? extends DataSource> superClassForDao) {
        this.superClassForDao = superClassForDao;
    }
    final Class<? extends DataSource> superClassForDao;
    private List<Class<? extends DataSource>> sourceClasslist = new ArrayList<>();
    private Class<? extends DataSource> currentSourceClass;
    private DataSource currentSource = null;
    private int defaultLocal = 0;
    //这个标记为指明该数据源是否激活，一般用于数据源调试，为False的数据源是不会被保存的

    public DataSource getDataSource() {
        if (currentSource == null) {
            /**
             * 数据源为空时的处理 一、检查数据源现用类是否为空，为空去数据源类列表的第一个，数据源列表都为空了只有返回Null
             * 二、用现用数据源实例化一个出来并且load();因为这是一个新的，所以要load()初始化数据
             *
             */
            if (currentSourceClass == null) {
                if (sourceClasslist.isEmpty()) {
                    return null;
                }
                /**
                 * 先看看是否有指定的默认数据源，没有就取第一个，前面已经进行了sourceClasslist的非空检查，所以，第一个一定有数据
                 */
                currentSourceClass=sourceClasslist.get(defaultLocal);
                if(currentSourceClass==null){
                    currentSourceClass = sourceClasslist.get(0);
                }
            }
            try {
                currentSource = currentSourceClass.newInstance();
                currentSource.load();
            } catch (InstantiationException | IllegalAccessException ex) {
                Logger.getLogger(DaoSource.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        } else {
            if (currentSource.getClass().equals(currentSourceClass)) {
                return currentSource;
            } else {
                /**
                 * 如果数据源的类和指定的现用数据源不一样的话，那么按现有数据源类重新实例化一个数据源出来，规则同上
                 */
                if (currentSourceClass == null) {
                    if (sourceClasslist.isEmpty()) {
                        return null;
                    }
                    currentSourceClass = sourceClasslist.get(0);
                }
                try {
                    currentSource = currentSourceClass.newInstance();
                    currentSource.load();
                } catch (InstantiationException | IllegalAccessException ex) {
                    Logger.getLogger(DaoSource.class.getName()).log(Level.SEVERE, null, ex);
                    return null;
                }
            }
        }
        System.out.println(this.name()+" default is :"+defaultLocal);
        return currentSource;
    }

    final public Class<? extends DataSource> getSuperClassForDao() {
        return superClassForDao;
    }

    public abstract boolean isActive();

    public Class<? extends DataSource> getDefaultDataSourceClass() {
        if (defaultLocal >= 0 && defaultLocal < sourceClasslist.size()) {
            return sourceClasslist.get(defaultLocal);
        }
        defaultLocal = sourceClasslist.indexOf(currentSourceClass);
        return currentSourceClass;
    }

    public List<Class<? extends DataSource>> getDataSourceClassList() {
        return sourceClasslist;
    }

    public void addDataSource(Class<? extends DataSource> c, boolean isDefault) {
        if (isDefault) {
            defaultLocal = sourceClasslist.size();
        }
        sourceClasslist.add(c);
    }

    public void addDataSource(Class<? extends DataSource> c) {
        addDataSource(c, false);
    }

    public void setDefaultDao(Class<? extends DataSource> c) {
        int local = sourceClasslist.indexOf(c);
        if (local > 0) {
            defaultLocal = local;
        }
    }

    /**
     * 改变现用的数据源类，不进行数据源的重新实例化，新实例化由getDataSource()时进行
     *
     * @param c
     */
    public void setCurrentDao(Class<? extends DataSource> c) {
        if (sourceClasslist.contains(c)) {
            currentSourceClass = c;
        }
    }

    public boolean isDefaultDao(Class<? extends DataSource> c) {
        return defaultLocal == sourceClasslist.indexOf(c);
    }

    public boolean isCurrentDao(Class<? extends DataSource> c) {
        return currentSourceClass == c;
    }

    //final static private File DaoConfig=new File("DaoConfig.cfg");
    static {
        DaoConfig.load();
    }

    public static DeviceDAO getDeviceDao() {
        return (hxg.portmgr.core.dao.DeviceDAO) DeviceDAO.getDataSource();
    }

    public static DeviceModelDAO getDeviceModelDAO() {
        return (hxg.portmgr.core.dao.DeviceModelDAO) DeviceModelDAO.getDataSource();
    }

    public static LineDAO getLineDao() {
        return (LineDAO) LineDAO.getDataSource();
    }

    public static LineProfileDAO getLineProfileDao() {
        return (hxg.portmgr.core.dao.LineProfileDAO) LineProfileDAO.getDataSource();
    }

    public static IntfDAO getIntfDAO() {
        return (hxg.portmgr.core.dao.IntfDAO) IntfDAO.getDataSource();
    }
}
